<?php
class IndexAction extends Action {
	public $act = array (
		'navtab' => array (
			'id' => 0,
			'close' => 0,
			'reload' => 0,
			'forward' => 0
		),
		'dialog' => array (
			'id' => 0,
			'close' => 0,
			'reload' => 0,
			'forward' => 0
		)
	);

	public function Index() {
		if (!isset ($_SESSION['uid'])) {
			$this->assign('jumpUrl', U('login'));
			redirect(U('login'));
			return;
		}

		$morder = M('Orderform');
		$orderformcount = array ();
		for ($i = 1; $i <= 6; $i++) {
			$orderformcount[$i] = $morder->where('progress=' . $i)->count();
		}
		$this->assign('orderformcount', $orderformcount);
		$mgood = M('good');
		$goodcount = $mgood->count();
		$goodpromotecount = $mgood->where('is_promote=1')->count();
		$goodlackcount = $mgood->where('is_lack=1')->count();
		$this->assign('goodcount', $goodcount);
		$this->assign('goodpromotecount', $goodpromotecount);
		$this->assign('goodlackcount', $goodlackcount);

		//$this->init_setting();
		$this->display();
	}

	public function login() {
		if (!$this->isPost()) {
			$this->display();
		} else {
			$username = isset ($_POST['username']) ? trim($_POST['username']) : '';
			if (strlen($username) < 5 || strlen($username) > 16) {
				$msg = "用户名为5到16个字符组成";
				$this->error($msg, 'username');
				return;
			}
			$tadmin = M("User");
			$condition['name'] = $username;
			$temp = $tadmin->where($condition)->find();
			if (!$temp && $_POST['ac'] != 'login') {
				$msg = "登录用户不存在或输入错误";
				$this->error($msg, 'username');
				return;
			}
			$password = isset ($_POST['password']) ? trim($_POST['password']) : '';
			if (strlen($password) < 6 || strlen($password) > 16) {
				$msg = "用户密码为6到16个字符组成";
				$this->error($msg, 'password');
				return;
			}
			if (md5($password) != $temp['password']) {
				$msg = "密码输入错误";
				$this->error($msg, 'password');
				return;
			}
			$verify = isset ($_POST['verify']) ? trim($_POST['verify']) : '';
			if (md5($verify) != Session :: get('verify')) {
				$msg = "请填写正确的验证码";
				$this->error($msg, 'verify');
				return;
			}

			Session :: set('uid', (int) $temp['user_id']);
			Session :: set('user_id', (int) $temp['user_id']);
			Session :: set('username', $username);
			Session :: set('wwwroot', __ROOT__);
			Session :: set('apppath', dirname(dirname(dirname(dirname(__FILE__)))). '/Uploads/');
			$this->success('', 1);
		}
	}
	public function logout() {
		$this->assign('jumpUrl', U('login'));
		Session :: clear();
		Session :: destroy();
		redirect(U('login'));
	}
	public function verify() {
		import("ORG.Util.Image");
		Image :: buildImageVerify();
	}
	public function set() {
		$m = M('Config');
		if ($this->isPost()) {
			$sets = isset ($_POST['set']) ? $_POST['set'] : null;
			foreach ($sets as $k => $v) {
				$map['name'] = $k;
				$data = array (
					'val' => $v
				);
				if ($k == 'syspass') {
					$data['val'] = md5(md5($v));
					if ($v == '') {
						$data['val'] = $m->where($map)->getField('val');
					}
				}

				$m->where($map)->save($data);
			}
			$this->setClose('dialog', 'Index_set');
			$this->success('修改成功', 1);
		} else {
			$set = isset ($_GET['set']) ? $_GET['set'] : 'sys';
			switch ($set) {
				case 'sys' :
					$map['type'] = 0;
					$data = $m->where($map)->select();
					break;
				case 'ser' :
					$map['type'] = 1;
					$data = $m->where($map)->select();
					break;
				default :
					break;
			}
			$this->assign('datalist', $data);
			$this->display();
		}

	}
	public function menu() {
		$menu = $_GET['id'];
		$this->display('Layout:b_menu_' . $menu);
	}
	public function cache() {
		if ($this->isPost()) {
			import("ORG.Io.Dir");
			$apppath = isset ($_POST['type']) ? $_POST['type'] : 'all';
			$path = array();
			if($apppath == 'all'){
				$fpath = './ServerAdmin';
				$path[] = $fpath . '/Runtime/Cache';
				$path[] = $fpath . '/Runtime/Temp';
				$path[] = $fpath . '/Runtime/Logs';
				$path[] = $fpath . '/Runtime/Data';
				$path[] = $fpath . '/Html/';
				$fpath = './ServerFront';
				$path[] = $fpath . '/Runtime/Cache';
				$path[] = $fpath . '/Runtime/Temp';
				$path[] = $fpath . '/Runtime/Logs';
				$path[] = $fpath . '/Runtime/Data';
				$path[] = $fpath . '/Html/';
			}else{
				$fpath = './' . $apppath;
				$path[] = $fpath . '/Runtime/Cache';
				$path[] = $fpath . '/Runtime/Temp';
				$path[] = $fpath . '/Runtime/Logs';
				$path[] = $fpath . '/Runtime/Data';
				$path[] = $fpath . '/Html/';
			}
			if (is_array($path)) {
			foreach ($path as $ap) {
					if (is_dir($ap))
						@Dir :: delDir($ap);
				}
			}
			$this->success('操作成功', 1);
		} else {
			$this->display();
		}

	}
	public function bksql() {
		$this->assign('mydbname', C('DB_NAME'));
		$this->assign('mydbuser', C('DB_USER'));
		$this->assign('mydbpwd', C('DB_PWD'));
		$this->display();
	}
	public function config() {
		$m = M('Config');
		if ($this->isPost()) {
			$sets = isset ($_POST['config']) ? $_POST['config'] : null;
			foreach ($sets as $k => $v) {
				$map['name'] = $k;
				$data = array (
					'val' => $v
				);
				$m->where($map)->save($data);
			}
			$this->setClose('dialog', 'Index_config');
			$this->success('修改成功', 1);
		} else {
			$datalist = $m->select();
			$this->assign('datalist', $datalist);
			$this->display();
		}
	}
	public function setClose($type = 'navtab', $id = '') {
		$this->act[$type]['close'] = 1;
		$this->act[$type]['id'] = $id;
	}
	public function setReload($type = 'navtab', $id = '') {
		$this->act[$type]['reload'] = 1;
		$this->act[$type]['id'] = $id;
	}
	public function setForward($url, $type = 'navtab', $id = '') {
		$this->act[$type]['forward'] = $url;
		$this->act[$type]['id'] = $id;
	}
	public function timeout() {
		$this->ajaxReturn('null', '会话超时', 301);
	}
	protected function ajaxReturn($data, $info = '', $status = 1, $type = 'json') {
		// 保证AJAX返回后也能保存日志
		if (C('LOG_RECORD'))
			Log :: save();
		$result = array ();
		$result['statusCode'] = $status;
		$result['navTabId'] = '';
		$result['callbackType'] = '';
		$result['forwardUrl'] = '';
		$result['message'] = $info;
		$result['data'] = $data;
		$result['act'] = $this->act;
		if (empty ($type))
			$type = C('DEFAULT_AJAX_RETURN');
		if (strtoupper($type) == 'JSON') {
			// 返回JSON数据格式到客户端 包含状态信息
			header("Content-Type:text/html; charset=utf-8");
			exit (json_encode($result));
		}
		elseif (strtoupper($type) == 'XML') {
			// 返回xml格式数据
			header("Content-Type:text/xml; charset=utf-8");
			exit (xml_encode($result));
		}
		elseif (strtoupper($type) == 'EVAL') {
			// 返回可执行的js脚本
			header("Content-Type:text/html; charset=utf-8");
			exit ($data);
		} else {
			// TODO 增加其它格式
		}
	}
}
?>